package com.jianbing.lakk.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSON;
import com.jianbing.common.core.domain.model.LoginUser;
import com.jianbing.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.jianbing.common.annotation.Log;
import com.jianbing.common.core.controller.BaseController;
import com.jianbing.common.core.domain.AjaxResult;
import com.jianbing.common.enums.BusinessType;
import com.jianbing.lakk.domain.LakkUserassociation;
import com.jianbing.lakk.service.ILakkUserassociationService;
import com.jianbing.common.utils.poi.ExcelUtil;
import com.jianbing.common.core.page.TableDataInfo;

/**
 * lakk用户关联Controller
 *
 * @author windbless
 * @date 2024-06-25
 */
@RestController
@RequestMapping("/lakk/UserAssociation")
public class LakkUserassociationController extends BaseController {
    @Autowired
    private ILakkUserassociationService lakkUserassociationService;


    @PostMapping("/addthis")
    public AjaxResult add1(@RequestBody LakkUserassociation lakkUserassociation1) {
        Long systemuserid = getappid(SecurityUtils.getLoginUser());
        // 创建一个新的 LakkUserassociation 实例并设置筛选条件
        LakkUserassociation filter = new LakkUserassociation();
        filter.setSystemuserid(systemuserid);
        // 根据筛选条件查询
        List<LakkUserassociation> list = lakkUserassociationService.selectLakkUserassociationList(filter);
        if (list.size() > 0) {
            return error("已绑定账号:" + list.get(0).getName() + ",请勿重复绑定");
        }
        //修改RequestBody中的systemuserid
        lakkUserassociation1.setSystemuserid(systemuserid);
        return toAjax(lakkUserassociationService.insertLakkUserassociation(lakkUserassociation1));
    }

    @GetMapping("/who_am_i")
    public TableDataInfo whoAmI() {

        Long systemuserid = getappid(SecurityUtils.getLoginUser());
        // 创建一个新的 LakkUserassociation 实例并设置筛选条件
        LakkUserassociation filter = new LakkUserassociation();
        filter.setSystemuserid(systemuserid);
        // 根据筛选条件查询
        List<LakkUserassociation> list = lakkUserassociationService.selectLakkUserassociationList(filter);
        if (list.size() == 0) {
            TableDataInfo test = new TableDataInfo();
            test.setCode(500);
            test.setMsg("未绑定账号");
            return test;
        }
        if (list.size() > 1) {
            TableDataInfo test = new TableDataInfo();
            test.setCode(500);
            test.setMsg("存在多条记录，请联系管理员");
            return test;
        }
        return getDataTable(list);
    }

    private Long getappid(LoginUser loginUser) {
        return loginUser.getUserId();
    }
//    解析用户信息

    /**
     * 查询lakk用户关联列表
     */
//    @PreAuthorize("@ss.hasPermi('lakk:UserAssociation:list')")
    @GetMapping("/list")
    public TableDataInfo list(LakkUserassociation lakkUserassociation) {
        startPage();
        List<LakkUserassociation> list = lakkUserassociationService.selectLakkUserassociationList(lakkUserassociation);
        return getDataTable(list);
    }

    /**
     * 导出lakk用户关联列表
     */
//    @PreAuthorize("@ss.hasPermi('lakk:UserAssociation:export')")
    @Log(title = "lakk用户关联", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LakkUserassociation lakkUserassociation) {
        List<LakkUserassociation> list = lakkUserassociationService.selectLakkUserassociationList(lakkUserassociation);
        ExcelUtil<LakkUserassociation> util = new ExcelUtil<LakkUserassociation>(LakkUserassociation.class);
        util.exportExcel(response, list, "lakk用户关联数据");
    }

    /**
     * 获取lakk用户关联详细信息
     */
//    @PreAuthorize("@ss.hasPermi('lakk:UserAssociation:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(lakkUserassociationService.selectLakkUserassociationById(id));
    }

    /**
     * 新增lakk用户关联
     */
//    @PreAuthorize("@ss.hasPermi('lakk:UserAssociation:add')")
    @Log(title = "lakk用户关联", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LakkUserassociation lakkUserassociation) {
        return toAjax(lakkUserassociationService.insertLakkUserassociation(lakkUserassociation));
    }

    /**
     * 修改lakk用户关联
     */
//    @PreAuthorize("@ss.hasPermi('lakk:UserAssociation:edit')")
    @Log(title = "lakk用户关联", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LakkUserassociation lakkUserassociation) {
        return toAjax(lakkUserassociationService.updateLakkUserassociation(lakkUserassociation));
    }

    /**
     * 删除lakk用户关联
     */
//    @PreAuthorize("@ss.hasPermi('lakk:UserAssociation:remove')")
    @Log(title = "lakk用户关联", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(lakkUserassociationService.deleteLakkUserassociationByIds(ids));
    }
}
